package com.stx.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.dao.DepartmentDao;
import com.stx.entity.Department;


/**
 * Servlet implementation class daddservlet
 */
public class daddservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public daddservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String dname= request.getParameter("dname");
		String daddress= request.getParameter("daddress");
		String dphone= request.getParameter("dphone");
		System.out.println(dname);
		DepartmentDao depdao= new DepartmentDao();
		if((dname=="")||(daddress=="")||(dphone=="")){
			request.setAttribute("errormsg", "信息不能为空");
			request.getRequestDispatcher("/admin//addd.jsp").forward(request, response);
		}
		else{
			if(depdao.addDepartment(dname, daddress, dphone)){
			List<Department> deplist=depdao.getAlldepartment();
			request.setAttribute("msg", "添加成功");
			request.setAttribute("deplist", deplist);
			request.getRequestDispatcher("/admin/department.jsp").forward(request, response);
		}else{
			request.setAttribute("errormsg", "添加失败，该科室已存在");
			request.getRequestDispatcher("/admin/addd.jsp").forward(request, response);
		}
		}
		
	}

}
